$(function () {
    $("#jqGrid").Grid({
        url: '../modulespdemandplan/list',
        colModel: [
            {label: '型芯计划名称', name: 'planName', index: 'PLAN_NAME'},
            {label: '需求车间', name: 'workshopName', index: 'WORKSHOP', width: 60},
            {label: '需求月份', name: 'demandMonth', index: 'DEMAND_MONTH', width: 60},
            {label: '中标厂家', name: 'supplierName', index: 'SUPPLIER', width: 80},
            {label: '总金额（元）', name: 'totalAmount', index: 'TOTAL_AMOUNT', width: 60},
            {label: '创建人', name: 'createUserName', index: 'CREATE_USER', width: 60},
            {
                label: '创建时间', name: 'gmtCreate', index: 'GMT_CREATE', width: 60, formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }
            },
            {
                label: '明细', width: 40, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button title='型芯详情' class='btn btn-outline btn-info' onclick='vm.goDevDetail(\"" + row.id + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true}
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleSpDemandPlan: {
            planName: '',
            id: '',
            remark: '',
            createUser: '',
            gmtCreate: '',
            workshop: '',
            demandMonth: '',
            totalAmount: '',
            supplier: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            workshop: '',
            demandMonth: ''
        },
        workshopList: [] // 所属车间下拉option列表
    },
    created: function () {
        this.loadWorshopList();
    },
    methods: {
        loadWorshopList: function () {
            var _this = this;
            Ajax.request({
                url: '../sys/dept/listByDeptLevel?deptLevel=2',
                async: true,
                successCallback: data => {
                    _this.workshopList = data.list;
                }
            });
        },
        goDevDetail: function (planId) {
            openWindow({
                type: 2,
                area:['100%','100%'],
                title: '<i class="fa fa-bars"></i> 型芯制作明细',
                content: '../module/modulespdemanditem.html?planId=' + planId
            })
        },
        query: function () {
            vm.reload();
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulespdemandplan/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulespdemandplan/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleSpDemandPlan = r.moduleSpDemandPlan;
                }
            });
        },
        reload: function (event) {
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            let queryParam = {};
            if (this.q.workshop) {
                queryParam.workshop = this.q.workshop;
            }
            if (this.q.demandMonth) {
                queryParam.demandMonth = gmt2MonthStr(this.q.demandMonth);
            }
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: queryParam,
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                workshop: '',
                demandMonth: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        exportData: function () {
            let paramArray = [];
            if (this.q.workshop) {
                paramArray.push('workshop=' + this.q.workshop);
            }
            if (this.q.demandMonth) {
                paramArray.push('demandMonth=' + gmt2MonthStr(this.q.demandMonth));
            }
            let exportUrl = '../modulespdemandplan/export'
            if(paramArray.length > 0){
                exportUrl += '?' + paramArray.join('&')
            }
            window.open(exportUrl, '_blank');
        }
    }
});
